package com.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.SysUserMapper;
import com.pojo.dto.Message;
import com.pojo.entity.SysUser;
import com.pojo.vo.SysUserVo;
import com.service.SysDictionaryService;
import com.service.SysUserService;
import com.util.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


@Service("SysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysDictionaryService sysDictionaryService;

    @Override
    public Message<List<SysUserVo>> list(SysUserVo sysUserVo) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(sysUserVo.getUsername())) {
            wrapper.like("username", sysUserVo.getUsername());//姓名模糊匹配
        }
        if (!StringUtils.isEmpty(sysUserVo.getNickname())) {
            wrapper.like("nickname", sysUserVo.getNickname());//身份模糊匹配
        }
        if (!StringUtils.isEmpty(sysUserVo.getPhone())) {
            wrapper.like("phone", sysUserVo.getPhone());//手机模糊匹配
        }
        if (!StringUtils.isEmpty(sysUserVo.getEmail())) {
            wrapper.like("email", sysUserVo.getEmail());//邮箱模糊匹配
        }
        List<SysUser> sysUsers = sysUserMapper.selectList(wrapper);

        return Message.successMessage((long) sysUsers.size(), PojoUtil.copyList(sysUsers, SysUserVo.class));

    }

    @Override
    public Message<SysUserVo> get(Long id) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        SysUser sysuser = sysUserMapper.selectOne(wrapper);
        return Message.successMessage(1L, PojoUtil.copyObject(sysuser, SysUserVo.class));
    }

    @Override
    public Message<SysUserVo> edit(SysUserVo sysUserVo) {
        SysUser sysUser = PojoUtil.copyObject(sysUserVo, SysUser.class);
        int i = sysUserMapper.updateById(sysUser);//根据ID更新book表
        if (i == 0) return Message.errorMessage("1001", "更新失败");
        else return Message.successMessage(1L, sysUserVo);
    }

    @Override
    public Message<SysUserVo> add(SysUserVo sysUserVo) {
        SysUser sysUser = PojoUtil.copyObject(sysUserVo, SysUser.class);
        int i = sysUserMapper.insert(sysUser);
        if (i == 0) return Message.errorMessage("1001", "添加失败");
        else return Message.successMessage(1L, sysUserVo);
    }

    @Override
    public Message<SysUserVo> delete(Long id) {
        int i = sysUserMapper.deleteById(id);
        if (i == 0) return Message.errorMessage("1001", "删除失败");
        else return Message.successMessage(1L, null);
    }
}
